
package it.bd.SetAllConnectionsOff.helper;

import java.lang.reflect.Method;

/**
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class CallerHelper {
    public static Object callGetter(Object x, String methodName) throws Exception {
        Class<?> c = x.getClass();
        Method[] theMethods = c.getMethods();
        for (int i = 0; i < theMethods.length; i++) {
            String methodString = theMethods[i].getName();
            if (methodString.trim().equalsIgnoreCase(methodName.trim())) {
                theMethods[i].setAccessible(true);
                return theMethods[i].invoke(x, new Object[] {});
            }
        }
        return null;
    }

    public static void callSetter(Object x, String methodName, Object... value) throws Exception {
        Class<?> c = x.getClass();
        Method[] theMethods = c.getMethods();
        for (int i = 0; i < theMethods.length; i++) {
            String methodString = theMethods[i].getName();
            if (methodString.trim().equalsIgnoreCase(methodName.trim())) {
                theMethods[i].setAccessible(true);
                theMethods[i].invoke(x, value);
            }
        }
    }

}
